package com.itheima.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    //在SpringBoot自动配置redis时，会提供创建RedisTemplate对象放入到IOC容器中，如果开发者自定义了RedisTemplate对象 则SpringBoot不会再进行提供
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        //1.创建RedisTemplate对象
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();

        //2.设置Redis存储key和value时使用String类型序列化器
        //redis默认的key序列化器为：JdkSerializationRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());

        //3.设置Redis连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //4.返回自定义的redisTemplate对象
        return  redisTemplate;
    }

}
